/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Mercado.controller;

import Mercado.model.Cadastro;
import Mercado.model.Cliente;
import java.awt.Color;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTable;
import javax.swing.JTextField;

/**
 *
 * @author rafael
 */
public class Clientes_controller {
    
    private Cadastro cad;

    public Clientes_controller(Cadastro cad) {
        this.cad = cad;
    }
    
    public void limpaCampo(JPanel aba){
        ((JTextField) aba.getComponent(0)).setText("");
        ((JTextField) aba.getComponent(1)).setText("");
        ((JTextField) aba.getComponent(2)).setText("");
        ((JLabel) aba.getComponent(3)).setText("");
        
    }
    
    public boolean executaCadastro(JPanel aba){
        String nome     = ((JTextField)aba.getComponent(0)).getText();
        String endereco = ((JTextField)aba.getComponent(1)).getText();
        String telefone = ((JTextField)aba.getComponent(2)).getText();
        if(nome.equals("")||endereco.equals("")||telefone.equals("")){
            System.err.println("Campos Vazios!");
            return false;
        }
        else{
            getCad().addCliente(new Cliente(nome, endereco, telefone));
            return true;
        }
    }
    
    public void btCadastro(JPanel aba){
        if (executaCadastro(aba)){
                limpaCampo(aba);
                ((JLabel)aba.getComponent(3)).setForeground(Color.green);
                ((JLabel)aba.getComponent(3)).setText("Cadastro efetuado com sucesso!");
            }
            else{
                ((JLabel)aba.getComponent(3)).setForeground(Color.red);
                ((JLabel)aba.getComponent(3)).setText("Preencha todos os campos!");
            }
    }

    public Cadastro getCad() {
        return cad;
    }
    
    public void setCad(Cadastro cad) {
        this.cad = cad;
    }
    
    public JTable geraTabela() {
        JTable tabela = new JTable();
        try{
            List<Cliente> cli = getCad().getClientes();
            String[][] rowData = new String[cli.size()][4];
            int i=0;
            for (Cliente c: cli){
                rowData[cli.indexOf(c)][0] = String.valueOf(i);
                rowData[cli.indexOf(c)][1] = c.getNome();
                rowData[cli.indexOf(c)][2] = c.getEndereco();
                rowData[cli.indexOf(c)][3] = c.getTelefone();
                i++;
            }
            String[] columnNames = {"Código","Nome","Endereço","Telefone"};
            tabela = new JTable(rowData, columnNames);
        }catch(Exception ex){
            tabela = new JTable();
        }finally{
            tabela.setBounds(5, 25, 620, 365);
            tabela.add(new JScrollBar());
        }
        return tabela;
    }

    public void atualizaTabela(JPanel lis) {
        try{
            lis.remove(4);
        }
        catch (ArrayIndexOutOfBoundsException ex){}
        lis.add(geraTabela());
        lis.getComponent(4).setVisible(false);
        lis.getComponent(4).setVisible(true);
    }
}
